home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevwdib.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  21.3 KB  |  742 lines

  1. /* Copyright (C) 1992, 1995, 1996, 1997, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevwdib.c,v 1.2 2000/09/19 19:00:23 lpd Exp $ */
  20. /* MS Windows 3.n driver for Ghostscript using a DIB for buffering. */
  21. #include "gdevmswn.h"
  22. #include "gxdevmem.h"
  23. #include "gsdll.h"
  24. #include "gsdllwin.h"
  25.  
  26. #ifdef __WIN32__
  27. #  define USE_SEGMENTS 0
  28. #else
  29. #  define USE_SEGMENTS 1
  30. #endif
  31.  
  32. /* Make sure we cast to the correct structure type. */
  33. typedef struct gx_device_win_dib_s gx_device_win_dib;
  34.  
  35. #undef wdev
  36. #define wdev ((gx_device_win_dib *)dev)
  37.  
  38. /* Device procedures */
  39.  
  40. /* See gxdevice.h for the definitions of the procedures. */
  41. private dev_proc_open_device(win_dib_open);
  42. private dev_proc_get_initial_matrix(win_dib_get_initial_matrix);
  43. private dev_proc_close_device(win_dib_close);
  44. private dev_proc_fill_rectangle(win_dib_fill_rectangle);
  45. private dev_proc_copy_mono(win_dib_copy_mono);
  46. private dev_proc_copy_color(win_dib_copy_color);
  47. private dev_proc_get_bits(win_dib_get_bits);
  48. private dev_proc_put_params(win_dib_put_params);
  49.  
  50. /* Windows-specific procedures */
  51. private win_proc_repaint(win_dib_repaint);
  52. private win_proc_alloc_bitmap(win_dib_alloc_bitmap);
  53. private win_proc_free_bitmap(win_dib_free_bitmap);
  54.  
  55. /* The device descriptor */
  56. struct gx_device_win_dib_s {
  57.     gx_device_common;
  58.     gx_device_win_common;
  59.  
  60. #if USE_SEGMENTS
  61.     /* The following help manage the division of the DIB */
  62.     /* into 64K segments.  Each block of y_block scan lines */
  63.     /* starting at y_base mod 64K falls in a single segment. */
  64.     /* Since the raster is a power of 2, y_block is a power of 2. */
  65.  
  66.     int y_block;
  67.     int y_base;
  68.     int y_mask;            /* y_block - 1 */
  69. #endif                /* USE_SEGMENTS */
  70.  
  71.     HGLOBAL hmdata;
  72. #ifdef __WIN32__
  73.     HANDLE hmtx;
  74. #endif
  75.     int lock_count;
  76.     gx_device_memory mdev;
  77. };
  78. private const gx_device_procs win_dib_procs =
  79. {
  80.     win_dib_open,
  81.     win_dib_get_initial_matrix,
  82.     win_sync_output,
  83.     win_output_page,
  84.     win_dib_close,
  85.     win_map_rgb_color,
  86.     win_map_color_rgb,
  87.     win_dib_fill_rectangle,
  88.     NULL,            /* tile_rectangle */
  89.     win_dib_copy_mono,
  90.     win_dib_copy_color,
  91.     NULL,            /* draw_line */
  92.     win_dib_get_bits /* NULL */ ,    /* get_bits */
  93.     win_get_params,
  94.     win_dib_put_params,
  95.     NULL,            /* map_cmyk_color */
  96.     win_get_xfont_procs,
  97.     NULL,            /* get_xfont_device */
  98.     NULL,            /* map_rgb_alpha_color */
  99.     gx_page_device_get_page_device
  100. };
  101. gx_device_win_dib far_data gs_mswindll_device =
  102. {
  103.     std_device_std_body(gx_device_win_dib, &win_dib_procs, "mswindll",
  104.             INITIAL_WIDTH, INITIAL_HEIGHT,    /* win_open() fills these in later */
  105.             INITIAL_RESOLUTION, INITIAL_RESOLUTION    /* win_open() fills these in later */
  106.     ),
  107.     {0},            /* std_procs */
  108.     0,                /* BitsPerPixel */
  109.     2,                /* nColors */
  110.     0,                /* mapped_color_flags */
  111.     win_dib_alloc_bitmap,
  112.     win_dib_free_bitmap
  113. };
  114.  
  115. /* forward declarations */
  116. private HGLOBAL win_dib_make_dib(gx_device_win * dev, int orgx, int orgy, int wx, int wy);
  117. private int win_dib_lock_device(unsigned char *device, int flag);
  118.  
  119.  
  120. /* Open the win_dib driver */
  121. private int
  122. win_dib_open(gx_device * dev)
  123. {
  124.     int code = win_open(dev);
  125.  
  126.     if (code < 0)
  127.     return code;
  128.  
  129. #ifdef __WIN32__
  130.     if (!is_win32s)
  131.     wdev->hmtx = CreateMutex(NULL, FALSE, NULL);    /* unnamed mutex, initially unowned */
  132. #endif
  133.     if (gdev_mem_device_for_bits(dev->color_info.depth) == 0) {
  134.     win_close(dev);
  135.     return gs_error_rangecheck;
  136.     }
  137.     code = win_dib_alloc_bitmap((gx_device_win *) dev, dev);
  138.     if (code < 0) {
  139.     win_close(dev);
  140.     return code;
  141.     }
  142.     /* notify caller about new device */
  143.     (*pgsdll_callback) (GSDLL_DEVICE, (unsigned char *)dev, 1);
  144.     (*pgsdll_callback) (GSDLL_SIZE, (unsigned char *)dev,
  145.             (dev->width & 0xffff) +
  146.             ((ulong) (dev->height & 0xffff) << 16));
  147.     return code;
  148. }
  149.  
  150. /* Get the initial matrix.  DIBs, unlike most displays, */
  151. /* put (0,0) in the lower left corner. */
  152. private void
  153. win_dib_get_initial_matrix(gx_device * dev, gs_matrix * pmat)
  154. {
  155.     pmat->xx = dev->x_pixels_per_inch / 72.0;
  156.     pmat->xy = 0.0;
  157.     pmat->yx = 0.0;
  158.     pmat->yy = dev->y_pixels_per_inch / 72.0;
  159.     pmat->tx = 0.0;
  160.     pmat->ty = 0.0;
  161. }
  162.  
  163. /* Close the win_dib driver */
  164. private int
  165. win_dib_close(gx_device * dev)
  166. {
  167.     int code;
  168.  
  169.     /* wait until bitmap is not being used by caller */
  170.     win_dib_lock_device((unsigned char *)dev, 1);
  171.     (*pgsdll_callback) (GSDLL_DEVICE, (unsigned char *)dev, 0);
  172.     win_dib_lock_device((unsigned char *)dev, 0);
  173.     win_dib_free_bitmap((gx_device_win *) dev);
  174. #ifdef __WIN32__
  175.     if (!is_win32s)
  176.     CloseHandle(wdev->hmtx);
  177. #endif
  178.     code = win_close(dev);
  179.     return code;
  180. }
  181.  
  182. #define wmdev ((gx_device *)&wdev->mdev)
  183. #define wmproc(proc) (*dev_proc(&wdev->mdev, proc))
  184.  
  185. #if USE_SEGMENTS
  186.  
  187. /* The drawing routines must all be careful not to cross */
  188. /* a segment boundary. */
  189.  
  190. #define single_block(y, h)\
  191.   !(((y - wdev->y_base) ^ (y - wdev->y_base + h - 1)) & ~wdev->y_mask)
  192.  
  193. #define BEGIN_BLOCKS\
  194. {    int by, bh, left = h;\
  195.     for ( by = y; left > 0; by += bh, left -= bh )\
  196.     {    bh = wdev->y_block - (by & wdev->y_mask);\
  197.         if ( bh > left ) bh = left;
  198. #define END_BLOCKS\
  199.     }\
  200. }
  201.  
  202. #endif /* (!)USE_SEGMENTS */
  203.  
  204. /* Fill a rectangle. */
  205. private int
  206. win_dib_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  207.                gx_color_index color)
  208. {
  209. #if USE_SEGMENTS
  210.     if (single_block(y, h)) {
  211.     wmproc(fill_rectangle) (wmdev, x, y, w, h, color);
  212.     } else {            /* Divide the transfer into blocks. */
  213.     BEGIN_BLOCKS
  214.         wmproc(fill_rectangle) (wmdev, x, by, w, bh, color);
  215.     END_BLOCKS
  216.     }
  217. #else
  218.     wmproc(fill_rectangle) (wmdev, x, y, w, h, color);
  219. #endif
  220.     return 0;
  221. }
  222.  
  223. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  224. /* Color = gx_no_color_index means transparent (no effect on the image). */
  225. private int
  226. win_dib_copy_mono(gx_device * dev,
  227.         const byte * base, int sourcex, int raster, gx_bitmap_id id,
  228.           int x, int y, int w, int h,
  229.           gx_color_index zero, gx_color_index one)
  230. {
  231. #if USE_SEGMENTS
  232.     if (single_block(y, h)) {
  233.     wmproc(copy_mono) (wmdev, base, sourcex, raster, id,
  234.                x, y, w, h, zero, one);
  235.     } else {            /* Divide the transfer into blocks. */
  236.     const byte *source = base;
  237.  
  238.     BEGIN_BLOCKS
  239.         wmproc(copy_mono) (wmdev, source, sourcex, raster,
  240.                    gx_no_bitmap_id, x, by, w, bh,
  241.                    zero, one);
  242.     source += bh * raster;
  243.     END_BLOCKS
  244.     }
  245. #else
  246.     wmproc(copy_mono) (wmdev, base, sourcex, raster, id,
  247.                x, y, w, h, zero, one);
  248. #endif
  249.     return 0;
  250. }
  251.  
  252. /* Copy a color pixel map.  This is just like a bitmap, except that */
  253. /* each pixel takes 8 or 4 bits instead of 1 when device driver has color. */
  254. private int
  255. win_dib_copy_color(gx_device * dev,
  256.         const byte * base, int sourcex, int raster, gx_bitmap_id id,
  257.            int x, int y, int w, int h)
  258. {
  259. #if USE_SEGMENTS
  260.     if (single_block(y, h)) {
  261.     wmproc(copy_color) (wmdev, base, sourcex, raster, id,
  262.                 x, y, w, h);
  263.     } else {            /* Divide the transfer into blocks. */
  264.     const byte *source = base;
  265.  
  266.     BEGIN_BLOCKS
  267.         wmproc(copy_color) (wmdev, source, sourcex, raster,
  268.                 gx_no_bitmap_id, x, by, w, bh);
  269.     source += by * raster;
  270.     END_BLOCKS
  271.     }
  272. #else
  273.     wmproc(copy_color) (wmdev, base, sourcex, raster, id,
  274.             x, y, w, h);
  275. #endif
  276.     return 0;
  277. }
  278.  
  279. int
  280. win_dib_get_bits(gx_device * dev, int y, byte * str, byte ** actual_data)
  281. {
  282.     return wmproc(get_bits) (wmdev, y, str, actual_data);
  283. }
  284.  
  285. int
  286. win_dib_put_params(gx_device * dev, gs_param_list * plist)
  287. {
  288.     int code;
  289.  
  290.     win_dib_lock_device((unsigned char *)dev, 1);
  291.     code = win_put_params(dev, plist);
  292.     win_dib_lock_device((unsigned char *)dev, 0);
  293.     return code;
  294. }
  295.  
  296. /* ------ DLL device procedures ------ */
  297.  
  298. /* make a copy of the device bitmap and return shared memory handle to it */
  299. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  300. HGLOBAL GSDLLAPI
  301. gsdll_copy_dib(unsigned char *device)
  302. {
  303.     gx_device_win_dib *dev = (gx_device_win_dib *) device;
  304.  
  305.     if (!dev || !dev->is_open || dev->mdev.width == 0 || dev->mdev.height == 0)
  306.     return (HGLOBAL) NULL;
  307.     return win_dib_make_dib((gx_device_win *) dev, 0, 0, dev->width, dev->height);
  308. }
  309.  
  310. /* make a copy of the device palette and return a handle to it */
  311. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  312. HPALETTE GSDLLAPI
  313. gsdll_copy_palette(unsigned char *device)
  314. {
  315.     gx_device_win_dib *dev = (gx_device_win_dib *) device;
  316.  
  317.     if (!dev || !dev->is_open || dev->mdev.width == 0 || dev->mdev.height == 0)
  318.     return (HPALETTE) NULL;
  319.     if (wdev->nColors > 0)
  320.     return CreatePalette(dev->limgpalette);
  321.     return (HPALETTE) NULL;
  322. }
  323.  
  324. /* copy the rectangle src from the device bitmap */
  325. /* to the rectangle dest on the device given by hdc */
  326. /* hdc must be a device context for a device (NOT a bitmap) */
  327. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  328. void GSDLLAPI
  329. gsdll_draw(unsigned char *device, HDC hdc, LPRECT dest, LPRECT src)
  330. {
  331.     gx_device_win_dib *dev = (gx_device_win_dib *) device;
  332.     HPALETTE oldpalette;
  333.  
  334.     if (!dev || !dev->is_open || dev->mdev.width == 0 || dev->mdev.height == 0)
  335.     return;
  336.     if (dev->nColors > 0) {
  337.     oldpalette = SelectPalette(hdc, dev->himgpalette, FALSE);
  338.     RealizePalette(hdc);
  339.     }
  340.     win_dib_repaint((gx_device_win *) dev, hdc, dest->left, dest->top,
  341.             dest->right - dest->left, dest->bottom - dest->top,
  342.             src->left, src->top);
  343.     if (dev->nColors > 0) {
  344.     SelectPalette(hdc, oldpalette, FALSE);
  345.     }
  346.     return;
  347. }
  348.  
  349. /* ------ Windows-specific device procedures ------ */
  350.  
  351.  
  352. /* Repaint a section of the window. */
  353. private void
  354. win_dib_repaint(gx_device_win * dev, HDC hdc, int dx, int dy, int wx, int wy,
  355.         int sx, int sy)
  356. {
  357.     struct bmi_s {
  358.     BITMAPINFOHEADER h;
  359.     ushort pal_index[256];
  360.     } bmi;
  361.     int i;
  362.     UINT which_colors;
  363.  
  364.     memset(&bmi.h, 0, sizeof(bmi.h));
  365.     
  366.     bmi.h.biSize = sizeof(bmi.h);
  367.     bmi.h.biWidth = wdev->mdev.width;
  368.     bmi.h.biHeight = wy;
  369.     bmi.h.biPlanes = 1;
  370.     bmi.h.biBitCount = dev->color_info.depth;
  371.     bmi.h.biCompression = 0;
  372.     bmi.h.biSizeImage = 0;    /* default */
  373.     bmi.h.biXPelsPerMeter = 0;    /* default */
  374.     bmi.h.biYPelsPerMeter = 0;    /* default */
  375.     
  376.     if (dev->BitsPerPixel <= 8) {
  377.     bmi.h.biClrUsed = wdev->nColors;
  378.     bmi.h.biClrImportant = wdev->nColors;
  379.     for (i = 0; i < wdev->nColors; i++)
  380.         bmi.pal_index[i] = i;
  381.     which_colors = DIB_PAL_COLORS;
  382.     } else if (dev->BitsPerPixel == 15) { /* 5-5-5 RGB mode */
  383.     DWORD* bmi_colors = (DWORD*)(&bmi.pal_index[0]);
  384.     bmi.h.biCompression = BI_BITFIELDS;
  385.     bmi_colors[0] = 0x7c00;
  386.     bmi_colors[1] = 0x03e0;
  387.     bmi_colors[2] = 0x001f;
  388.     which_colors = DIB_RGB_COLORS;
  389.     } else if (dev->BitsPerPixel == 16) { /* 5-6-5 RGB mode */
  390.     DWORD* bmi_colors = (DWORD*)(&bmi.pal_index[0]);
  391.     bmi.h.biCompression = BI_BITFIELDS;
  392.     bmi_colors[0] = 0xf800;
  393.     bmi_colors[1] = 0x07e0;
  394.     bmi_colors[2] = 0x001f;
  395.     which_colors = DIB_RGB_COLORS;
  396.     } else {
  397.     bmi.h.biClrUsed = 0;
  398.     bmi.h.biClrImportant = 0;
  399.     which_colors = DIB_RGB_COLORS;
  400.     }
  401.     /*
  402.      * Windows apparently limits the size of a single transfer
  403.      * to 2 Mb, which can be exceeded on 24-bit displays.
  404.      * Deal with this here.
  405.      */
  406. #define max_transfer 2000000
  407.     if (wdev->mdev.raster > 0) {    /* just in case! */
  408.     long ny = max_transfer / wdev->mdev.raster;
  409.  
  410.     for (; wy > ny; dy += ny, wy -= ny, sy += ny)
  411.         SetDIBitsToDevice(hdc, dx, dy, wx, ny,
  412.                   sx, 0, 0, ny,
  413.                   wdev->mdev.line_ptrs[wdev->height - (sy + ny)],
  414.                   (BITMAPINFO FAR *) & bmi, which_colors);
  415.     }
  416. #undef max_transfer
  417.     SetDIBitsToDevice(hdc, dx, dy, wx, wy,
  418.               sx, 0, 0, wy,
  419.               wdev->mdev.line_ptrs[wdev->height - (sy + wy)],
  420.               (BITMAPINFO FAR *) & bmi, which_colors);
  421. }
  422.  
  423. /* This makes a DIB that contains all or part of the bitmap. */
  424. /* The bitmap pixel orgx must start on a byte boundary. */
  425. private HGLOBAL
  426. win_dib_make_dib(gx_device_win * dev, int orgx, int orgy, int wx, int wy)
  427. {
  428. #define xwdev ((gx_device_win_dib *)dev)
  429.     gx_color_value prgb[3];
  430.     HGLOBAL hglobal;
  431.     BYTE FAR *pDIB;
  432.     BITMAPINFOHEADER FAR *pbmih;
  433.     RGBQUAD FAR *pColors;
  434.     BYTE FAR *pBits;
  435.     BYTE FAR *pLine;
  436.     ulong bitmapsize;
  437.     int palcount;
  438.     int i;
  439.     UINT lwidth;        /* line width in bytes rounded up to multiple of 4 bytes */
  440.  
  441. #ifdef USE_SEGMENTS
  442.     int loffset;        /* byte offset to start of line */
  443.     UINT lseg;            /* bytes remaining in this segment */
  444.  
  445. #endif
  446.  
  447.     if (orgx + wx > wdev->width)
  448.     wx = wdev->width - orgx;
  449.     if (orgy + wy > wdev->height)
  450.     wy = wdev->height - orgy;
  451.  
  452.     loffset = orgx * wdev->color_info.depth / 8;
  453.     lwidth = ((wx * wdev->color_info.depth + 31) & ~31) >> 3;
  454.     bitmapsize = (long)lwidth *wy;
  455.  
  456.     if (wdev->color_info.depth > 16)
  457.     palcount = 0;
  458.     else if (wdev->color_info.depth > 8)
  459.     palcount = 3;        // 16-bit BI_BITFIELDS
  460.     else
  461.     palcount = wdev->nColors;
  462.  
  463.     hglobal = GlobalAlloc(GHND | GMEM_SHARE, sizeof(BITMAPINFOHEADER)
  464.               + sizeof(RGBQUAD) * palcount + bitmapsize);
  465.     if (hglobal == (HGLOBAL) NULL) {
  466.     MessageBeep(-1);
  467.     return (HGLOBAL) NULL;
  468.     }
  469.     pDIB = (BYTE FAR *) GlobalLock(hglobal);
  470.     if (pDIB == (BYTE FAR *) NULL) {
  471.     MessageBeep(-1);
  472.     return (HGLOBAL) NULL;
  473.     }
  474.     pbmih = (BITMAPINFOHEADER FAR *) (pDIB);
  475.     pColors = (RGBQUAD FAR *) (pDIB + sizeof(BITMAPINFOHEADER));
  476.     pBits = (BYTE FAR *) (pDIB + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * palcount);
  477.  
  478.     pbmih->biSize = sizeof(BITMAPINFOHEADER);
  479.     pbmih->biWidth = wx;
  480.     pbmih->biHeight = wy;
  481.     pbmih->biPlanes = 1;
  482.     pbmih->biBitCount = wdev->color_info.depth;
  483.     pbmih->biCompression = 0;
  484.     pbmih->biSizeImage = 0;    /* default */
  485.     pbmih->biXPelsPerMeter = (DWORD) (dev->x_pixels_per_inch / 25.4 * 1000);
  486.     pbmih->biYPelsPerMeter = (DWORD) (dev->y_pixels_per_inch / 25.4 * 1000);
  487.     pbmih->biClrUsed = palcount;
  488.     pbmih->biClrImportant = palcount;
  489.  
  490.     if (dev->BitsPerPixel == 15) { /* 5-5-5 RGB mode */
  491.     DWORD* bmi_colors = (DWORD*)(pColors);
  492.         pbmih->biCompression = BI_BITFIELDS;
  493.     bmi_colors[0] = 0x7c00;
  494.     bmi_colors[1] = 0x03e0;
  495.     bmi_colors[2] = 0x001f;
  496.     } 
  497.     else if (dev->BitsPerPixel == 16) { /* 5-6-5 RGB mode */
  498.     DWORD* bmi_colors = (DWORD*)(pColors);
  499.         pbmih->biCompression = BI_BITFIELDS;
  500.     bmi_colors[0] = 0xf800;
  501.     bmi_colors[1] = 0x07e0;
  502.     bmi_colors[2] = 0x001f;
  503.     } 
  504.     else {
  505.     for (i = 0; i < palcount; i++) {
  506.     win_map_color_rgb((gx_device *) wdev, (gx_color_index) i, prgb);
  507.     pColors[i].rgbRed = win_color_value(prgb[0]);
  508.     pColors[i].rgbGreen = win_color_value(prgb[1]);
  509.     pColors[i].rgbBlue = win_color_value(prgb[2]);
  510.     pColors[i].rgbReserved = 0;
  511.     }
  512.     }
  513.  
  514.     pLine = pBits;
  515.     for (i = orgy; i < orgy + wy; i++) {
  516. #if USE_SEGMENTS
  517.     /* Window 3.1 has hmemcpy, but 3.0 doesn't */
  518.     lseg = (UINT) (-OFFSETOF(pLine));    /* remaining bytes in this segment */
  519.     if (lseg >= lwidth) {
  520.         _fmemcpy(pLine, xwdev->mdev.line_ptrs[i] + loffset, lwidth);
  521.     } else {        /* break up transfer to avoid crossing segment boundary */
  522.         _fmemcpy(pLine, xwdev->mdev.line_ptrs[i] + loffset, lseg);
  523.         _fmemcpy(pLine + lseg, xwdev->mdev.line_ptrs[i] + loffset + lseg, lwidth - lseg);
  524.     }
  525. #else
  526.     memcpy(pLine, xwdev->mdev.line_ptrs[i], lwidth);
  527. #endif
  528.     pLine += lwidth;
  529.     }
  530.  
  531.     GlobalUnlock(hglobal);
  532.     return hglobal;
  533. }
  534.  
  535.  
  536. /* Allocate the backing bitmap. */
  537. private int
  538. win_dib_alloc_bitmap(gx_device_win * dev, gx_device * param_dev)
  539. {
  540.     int width;
  541.     gx_device_memory mdev;
  542.     HGLOBAL hmdata;
  543.     byte FAR *base;
  544.     byte FAR *ptr_base;
  545.     uint ptr_size;
  546.     uint raster;
  547.  
  548. #ifdef USE_SEGMENTS
  549.     ulong data_size;
  550.  
  551. #endif
  552.  
  553. #ifdef __WIN32__
  554.     if (is_win32s) {
  555. #endif
  556.     /* Round up the width so that the scan line size is a power of 2. */
  557.     if (dev->color_info.depth == 24) {
  558.         width = param_dev->width * 3 - 1;
  559.         while (width & (width + 1))
  560.         width |= width >> 1;
  561.         width = (width + 1) / 3;
  562.     } else {
  563.         width = param_dev->width - 1;
  564.         while (width & (width + 1))
  565.         width |= width >> 1;
  566.         width++;
  567.     }
  568. #ifdef __WIN32__
  569.     } else {            /* don't have to worry about segments so use less memory */
  570.     width = param_dev->width;
  571.     }
  572. #endif
  573.  
  574.     /* Finish initializing the DIB. */
  575.  
  576.     gs_make_mem_device(&mdev, gdev_mem_device_for_bits(dev->color_info.depth), 0, 0, (gx_device *) dev);
  577.     mdev.width = width;
  578.     mdev.height = param_dev->height;
  579.     raster = gdev_mem_raster(&mdev);
  580.     data_size = (ulong) raster *mdev.height;
  581.  
  582.     ptr_size = sizeof(byte **) * mdev.height;
  583.     hmdata = GlobalAlloc(0, raster + data_size + ptr_size * 2);
  584.     if (hmdata == 0) {
  585.     return win_nomemory();
  586.     }
  587.     /* Nothing can go wrong now.... */
  588.  
  589.     wdev->hmdata = hmdata;
  590.     base = GlobalLock(hmdata);
  591. #if USE_SEGMENTS
  592.     /* Adjust base so scan lines, and the pointer table, */
  593.     /* don't cross a segment boundary. */
  594.     base += (-PTR_OFF(base) & (raster - 1));
  595.     ptr_base = base + data_size;
  596.     if (PTR_OFF(ptr_base + ptr_size) < ptr_size)
  597.     base += (uint) - PTR_OFF(ptr_base);
  598.     wdev->y_block = 0x10000L / raster;
  599.     wdev->y_mask = wdev->y_block - 1;
  600.     if ((wdev->y_base = PTR_OFF(base)) != 0)
  601.     wdev->y_base = -(PTR_OFF(base) / raster);
  602. #endif
  603.     wdev->mdev = mdev;
  604.     wdev->mdev.base = (byte *) base;
  605.     wmproc(open_device) ((gx_device *) & wdev->mdev);
  606.  
  607.     if (wdev->is_open)
  608.     (*pgsdll_callback) (GSDLL_SIZE, (unsigned char *)dev,
  609.                 (dev->width & 0xffff) +
  610.                 ((ulong) (dev->height & 0xffff) << 16));
  611.  
  612.     return 0;
  613. }
  614.  
  615.  
  616. /* Free the backing bitmap. */
  617. private void
  618. win_dib_free_bitmap(gx_device_win * dev)
  619. {
  620.     HGLOBAL hmdata = wdev->hmdata;
  621.  
  622.     GlobalUnlock(hmdata);
  623.     GlobalFree(hmdata);
  624. }
  625.  
  626. /* Lock the device (so it's size cannot be changed) if flag = TRUE */
  627. /* or unlock the device if flag = FALSE */
  628. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  629. private int
  630. win_dib_lock_device(unsigned char *device, int flag)
  631. {
  632.     gx_device *dev = (gx_device *) device;
  633.  
  634. #ifdef __WIN32__
  635.     if (!is_win32s) {
  636.     if (flag) {
  637.         if (WaitForSingleObject(wdev->hmtx, 60000) == WAIT_TIMEOUT)
  638.         return 2;
  639.         return 1;
  640.     }
  641.     ReleaseMutex(wdev->hmtx);
  642.     return 0;
  643.     }
  644. #endif
  645.     if (flag)
  646.     wdev->lock_count++;
  647.     else
  648.     wdev->lock_count--;
  649.     if (wdev->lock_count < 0)
  650.     wdev->lock_count = 0;
  651.     return wdev->lock_count;
  652. }
  653.  
  654. int GSDLLAPI _export
  655. gsdll_lock_device(unsigned char *device, int flag)
  656. {
  657.     return win_dib_lock_device(device, flag);
  658. }
  659.  
  660.  
  661. /* Copy bitmap
  662.  * If pbmih nonzero, copy the BITMAPINFOHEADER.
  663.  * If prgbquad nonzero, copy the palette.
  664.  *   number of entries copied is given by pbmih->biClrUsed
  665.  * If ppbyte nonzero, return pointer to row.
  666.  *   pointer is only valid while device is locked
  667.  * GS can change the palette while the device is locked.
  668.  * Do not call this function while GS is busy.
  669.  * If all pbmih and prgbquad and ppbyte are all NULL,
  670.  * return value is byte count needed for BITMAPINFOHEADER
  671.  * and palette and one bitmap row.
  672.  * Otherwise return value is 0;
  673.  *
  674.  * This function exists to allow the bitmap to be copied to a file
  675.  * or structured storage, without the overhead of having two copies
  676.  * of the bitmap in memory at the same time.
  677.  */
  678. int GSDLLAPI _export
  679. gsdll_get_bitmap_row(unsigned char *device, LPBITMAPINFOHEADER pbmih,
  680.              LPRGBQUAD prgbquad, LPBYTE * ppbyte, unsigned int row)
  681. {
  682.     int palcount;
  683.     gx_device_win_dib *dev = (gx_device_win_dib *) device;
  684.  
  685.     palcount = (dev->color_info.depth == 24) ? 0 : dev->nColors;
  686.  
  687.     if (pbmih) {
  688.     pbmih->biSize = sizeof(BITMAPINFOHEADER);
  689.     pbmih->biWidth = dev->width;
  690.     pbmih->biHeight = dev->mdev.height;
  691.     pbmih->biPlanes = 1;
  692.     pbmih->biBitCount = dev->color_info.depth;
  693.     if ((dev->BitsPerPixel == 15) || (dev->BitsPerPixel == 16))
  694.             pbmih->biCompression = BI_BITFIELDS;
  695.     else
  696.     pbmih->biCompression = 0;
  697.     pbmih->biSizeImage = 0;    /* default */
  698.     pbmih->biXPelsPerMeter = (DWORD) (dev->x_pixels_per_inch / 25.4 * 1000);
  699.     pbmih->biYPelsPerMeter = (DWORD) (dev->y_pixels_per_inch / 25.4 * 1000);
  700.     pbmih->biClrUsed = palcount;
  701.     pbmih->biClrImportant = palcount;
  702.     }
  703.     if (prgbquad) {
  704.     int i;
  705.     gx_color_value prgb[3];
  706.  
  707.     if (dev->BitsPerPixel == 15) { /* 5-5-5 RGB mode */
  708.         DWORD* bmi_colors = (DWORD*)(prgbquad);
  709.         pbmih->biCompression = BI_BITFIELDS;
  710.         bmi_colors[0] = 0x7c00;
  711.         bmi_colors[1] = 0x03e0;
  712.         bmi_colors[2] = 0x001f;
  713.     } 
  714.     else if (dev->BitsPerPixel == 16) { /* 5-6-5 RGB mode */
  715.         DWORD* bmi_colors = (DWORD*)(prgbquad);
  716.         pbmih->biCompression = BI_BITFIELDS;
  717.         bmi_colors[0] = 0xf800;
  718.         bmi_colors[1] = 0x07e0;
  719.         bmi_colors[2] = 0x001f;
  720.     } 
  721.     else {
  722.     for (i = 0; i < palcount; i++) {
  723.         win_map_color_rgb((gx_device *) wdev, (gx_color_index) i, prgb);
  724.         prgbquad[i].rgbRed = win_color_value(prgb[0]);
  725.         prgbquad[i].rgbGreen = win_color_value(prgb[1]);
  726.         prgbquad[i].rgbBlue = win_color_value(prgb[2]);
  727.         prgbquad[i].rgbReserved = 0;
  728.         }
  729.     }
  730.     }
  731.     if (ppbyte) {
  732.     if (row < dev->mdev.height)
  733.         *ppbyte = dev->mdev.line_ptrs[row];
  734.     else
  735.         *ppbyte = NULL;
  736.     }
  737.     if ((pbmih == NULL) && (prgbquad == NULL) && (ppbyte == NULL))
  738.     return sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)
  739.         + gdev_mem_raster(&(dev->mdev));
  740.     return 0;
  741. }
  742.